<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: Where</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">Where</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../files/vendor/plugins/railswhere/lib/where_rb.html">
                vendor/plugins/railswhere/lib/where.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                Object
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <h1><a href="Where.html">Where</a> clause generator</h1>
<h2>Author: Tim Harper ( &quot;timseeharperATgmail.seeom&quot;.gsub(&quot;see&quot;, &quot;c&quot;).gsub(&quot;AT&quot;, &quot;@&quot;) )</h2>
<p>
<b>Usage example</b>
</p>
<h3>Returning SQL</h3>
<pre>
 sql = Where.new('x=?',5).and( Where.new('x=?',6).or('x=?',7)).to_s
 # returns (x=5) and ( ( x=6 ) or ( x=7 ) )
</pre>
<h3>Building a complicated where clause made easy</h3>
<pre>
 def get_search_query_string

   where = Where.new
   where.and('users.first_name like ?', params[:search_first_name] + '%') unless params[:search_first_name].blank?
   where.and('users.last_name like ?', params[:search_last_name] + '%') unless params[:search_last_name].blank?

   status_where = Where.new
   for status in params[search_statuses].split(',')
     status_where.or 'status=?', status
   end
   where.and status_where unless status_where.blank?

   where.to_s
 end
</pre>
<p>
User.find(:all, :conditions =&gt; get_search_query_string)
</p>
<h3>Inline</h3>
<pre>
   User.find(:all, :conditions =&gt; Where.new('first_name like ?', 'Tim').and('last_name like ?', 'Harper') )
   # Sweet chaining action!
</pre>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000009">&amp;</a>&nbsp;&nbsp;
      <a href="#M000011">&amp;</a>&nbsp;&nbsp;
      <a href="#M000005">&lt;&lt;</a>&nbsp;&nbsp;
      <a href="#M000004">and</a>&nbsp;&nbsp;
      <a href="#M000008">and_not</a>&nbsp;&nbsp;
      <a href="#M000015">blank?</a>&nbsp;&nbsp;
      <a href="#M000016">empty?</a>&nbsp;&nbsp;
      <a href="#M000003">initialize_copy</a>&nbsp;&nbsp;
      <a href="#M000002">new</a>&nbsp;&nbsp;
      <a href="#M000006">or</a>&nbsp;&nbsp;
      <a href="#M000007">or_not</a>&nbsp;&nbsp;
      <a href="#M000013">to_s</a>&nbsp;&nbsp;
      <a href="#M000014">to_sql</a>&nbsp;&nbsp;
      <a href="#M000010">|</a>&nbsp;&nbsp;
      <a href="#M000012">|</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">

    <div id="class-list">
      <h3 class="section-bar">Classes and Modules</h3>

      Class <a href="Where/Clause.html" class="link">Where::Clause</a><br />

    </div>




      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M000011" class="method-detail">
        <a name="M000011"></a>

        <div class="method-heading">
          <a href="#M000011" class="method-signature">
          <span class="method-name">&amp;</span><span class="method-args">(params)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000011-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000011-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/plugins/railswhere/lib/where.rb, line 101</span>
101:   <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-operator">&amp;</span>(<span class="ruby-identifier">params</span>)
102:     <span class="ruby-constant">Where</span>.<span class="ruby-identifier">new</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">params</span>)
103:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000002" class="method-detail">
        <a name="M000002"></a>

        <div class="method-heading">
          <a href="#M000002" class="method-signature">
          <span class="method-name">new</span><span class="method-args">(criteria=nil, *params) {|self| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Constructs a <a href="Where.html#M000002">new</a> where clause
</p>
<p>
optionally, you can provide a criteria, like the following:
</p>
<pre>
  Where.initialize &quot;joke_title = ?&quot;, &quot;He says, 'Under there', to which I reply, 'under where?'&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000002-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/railswhere/lib/where.rb, line 42</span>
42:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">criteria</span>=<span class="ruby-keyword kw">nil</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">params</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
43:     <span class="ruby-ivar">@clauses</span>=<span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>
44:     
45:     <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">and</span>(<span class="ruby-identifier">criteria</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">params</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">criteria</span>.<span class="ruby-identifier">nil?</span>
46:     
47:     <span class="ruby-keyword kw">yield</span>(<span class="ruby-keyword kw">self</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
48:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000012" class="method-detail">
        <a name="M000012"></a>

        <div class="method-heading">
          <a href="#M000012" class="method-signature">
          <span class="method-name">|</span><span class="method-args">(params)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000012-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000012-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/plugins/railswhere/lib/where.rb, line 105</span>
105:   <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-operator">|</span>(<span class="ruby-identifier">params</span>)
106:     <span class="ruby-constant">Where</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">or</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">params</span>)
107:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000009" class="method-detail">
        <a name="M000009"></a>

        <div class="method-heading">
          <a href="#M000009" class="method-signature">
          <span class="method-name">&amp;</span><span class="method-args">(params)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000009-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000009-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/railswhere/lib/where.rb, line 93</span>
93:   <span class="ruby-keyword kw">def</span> <span class="ruby-operator">&amp;</span>(<span class="ruby-identifier">params</span>)
94:     <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">and</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">params</span>)
95:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000005" class="method-detail">
        <a name="M000005"></a>

        <div class="method-heading">
          <span class="method-name">&lt;&lt;</span><span class="method-args">(*params, &amp;block)</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="Where.html#M000004">and</a>
</p>
        </div>
      </div>

      <div id="method-M000004" class="method-detail">
        <a name="M000004"></a>

        <div class="method-heading">
          <a href="#M000004" class="method-signature">
          <span class="method-name">and</span><span class="method-args">(*params, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Appends an <b><a href="Where.html#M000004">and</a></b> expression to your
where clause
</p>
<p>
Example:
</p>
<pre>
  where = Where.new
  where.and(&quot;name = ?&quot;, &quot;Tim O'brien&quot;)
  where.to_s

  # =&gt; &quot;(name = 'Tim O''brien')
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000004-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/railswhere/lib/where.rb, line 63</span>
63:   <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">and</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">params</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
64:     <span class="ruby-identifier">append_clause</span>(<span class="ruby-identifier">params</span>, <span class="ruby-value str">&quot;AND&quot;</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
65:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000008" class="method-detail">
        <a name="M000008"></a>

        <div class="method-heading">
          <a href="#M000008" class="method-signature">
          <span class="method-name">and_not</span><span class="method-args">(*params, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Same as <a href="Where.html#M000004">and</a>, but negates the whole
expression
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000008-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000008-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/railswhere/lib/where.rb, line 89</span>
89:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">and_not</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">params</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
90:     <span class="ruby-identifier">append_clause</span>(<span class="ruby-identifier">params</span>, <span class="ruby-value str">&quot;AND NOT&quot;</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
91:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000015" class="method-detail">
        <a name="M000015"></a>

        <div class="method-heading">
          <a href="#M000015" class="method-signature">
          <span class="method-name">blank?</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Determines if any clauses have been added.
</p>
<pre>
  where = Where.new
  where.blank?
  # =&gt; true

  where.and(nil)
  where.blank?
  # =&gt; true

  where.and(Where.new(nil))
  where.blank?
  # =&gt; true

  where.and(&quot;name=1&quot;)
  where.blank?
  # =&gt; false
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000015-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000015-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/plugins/railswhere/lib/where.rb, line 144</span>
144:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">blank?</span>
145:     <span class="ruby-ivar">@clauses</span>.<span class="ruby-identifier">empty?</span>
146:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000016" class="method-detail">
        <a name="M000016"></a>

        <div class="method-heading">
          <span class="method-name">empty?</span><span class="method-args">()</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for blank?
</p>
        </div>
      </div>

      <div id="method-M000003" class="method-detail">
        <a name="M000003"></a>

        <div class="method-heading">
          <a href="#M000003" class="method-signature">
          <span class="method-name">initialize_copy</span><span class="method-args">(from)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000003-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/railswhere/lib/where.rb, line 50</span>
50:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize_copy</span>(<span class="ruby-identifier">from</span>)
51:     <span class="ruby-ivar">@clauses</span> = <span class="ruby-identifier">from</span>.<span class="ruby-identifier">instance_variable_get</span>(<span class="ruby-value str">&quot;@clauses&quot;</span>).<span class="ruby-identifier">clone</span>
52:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000006" class="method-detail">
        <a name="M000006"></a>

        <div class="method-heading">
          <a href="#M000006" class="method-signature">
          <span class="method-name">or</span><span class="method-args">(*params, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Appends an <b><a href="Where.html#M000006">or</a></b> expression to your
where clause
</p>
<p>
Example:
</p>
<pre>
  where = Where.new
  where.or(&quot;name = ?&quot;, &quot;Tim O'brien&quot;)
  where.or(&quot;name = ?&quot;, &quot;Tim O'neal&quot;)
  where.to_s

  # =&gt; &quot;(name = 'Tim O''brien') or (name = 'Tim O''neal')&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000006-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000006-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/railswhere/lib/where.rb, line 79</span>
79:   <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">or</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">params</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
80:     <span class="ruby-identifier">append_clause</span>(<span class="ruby-identifier">params</span>, <span class="ruby-value str">&quot;OR&quot;</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
81:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000007" class="method-detail">
        <a name="M000007"></a>

        <div class="method-heading">
          <a href="#M000007" class="method-signature">
          <span class="method-name">or_not</span><span class="method-args">(*params, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Same as <a href="Where.html#M000006">or</a>, but negates the whole
expression
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000007-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000007-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/railswhere/lib/where.rb, line 84</span>
84:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">or_not</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">params</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
85:     <span class="ruby-identifier">append_clause</span>(<span class="ruby-identifier">params</span>, <span class="ruby-value str">&quot;OR NOT&quot;</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
86:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000013" class="method-detail">
        <a name="M000013"></a>

        <div class="method-heading">
          <a href="#M000013" class="method-signature">
          <span class="method-name">to_s</span><span class="method-args">(format=nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Converts the where clause to a SQL string.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000013-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000013-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/plugins/railswhere/lib/where.rb, line 110</span>
110:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_s</span>(<span class="ruby-identifier">format</span>=<span class="ruby-keyword kw">nil</span>)
111:     <span class="ruby-identifier">output</span>=<span class="ruby-value str">&quot;&quot;</span>
112:     
113:     <span class="ruby-ivar">@clauses</span>.<span class="ruby-identifier">each_index</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
114:       <span class="ruby-identifier">omit_conjuction</span> = (<span class="ruby-identifier">index</span><span class="ruby-operator">==</span><span class="ruby-value">0</span>)
115:       <span class="ruby-identifier">output</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@clauses</span>[<span class="ruby-identifier">index</span>].<span class="ruby-identifier">to_s</span>(<span class="ruby-identifier">omit_conjuction</span>)  <span class="ruby-comment cmt"># Omit the clause if index=0</span>
116:     }
117:     <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">format</span>
118:     <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:where</span>
119:       <span class="ruby-identifier">output</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-value">? </span><span class="ruby-value str">&quot;&quot;</span> <span class="ruby-operator">:</span> <span class="ruby-node">&quot; WHERE #{output}&quot;</span>
120:     <span class="ruby-keyword kw">else</span>
121:       <span class="ruby-identifier">output</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-value">? </span><span class="ruby-keyword kw">nil</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">output</span>
122:     <span class="ruby-keyword kw">end</span>
123:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000014" class="method-detail">
        <a name="M000014"></a>

        <div class="method-heading">
          <span class="method-name">to_sql</span><span class="method-args">(format=nil)</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="Where.html#M000013">to_s</a>
</p>
        </div>
      </div>

      <div id="method-M000010" class="method-detail">
        <a name="M000010"></a>

        <div class="method-heading">
          <a href="#M000010" class="method-signature">
          <span class="method-name">|</span><span class="method-args">(params)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000010-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000010-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/railswhere/lib/where.rb, line 97</span>
97:   <span class="ruby-keyword kw">def</span> <span class="ruby-operator">|</span>(<span class="ruby-identifier">params</span>)
98:     <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">or</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">params</span>)
99:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>